/*
 * @Author: szx
 * @Date: 2021-12-15 13:32:19
 * @LastEditTime: 2021-12-15 13:48:12
 * @Description:
 * @FilePath: \leetcode\100-199\144\144.js
 */
function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
// 递归
var preorderTraversal = function (root) {
    const res = [];
    const dfs = (node) => {
        if (node === null) return;
        res.push(node.val);
        dfs(node.left);
        dfs(node.right);
    };
    dfs(root);
    return res;
};

//迭代
var preorderTraversal = function (root) {
    const res = [];
    if (root == null) return [];
    const stk = [];
    stk.push(root);
    while (stk.length != 0) {
        let tmp = stk.pop();
        res.push(tmp.val);
        if (tmp.right) stk.push(tmp.right);
        if (tmp.left) stk.push(tmp.left);
    }
    return res;
};
